﻿using System;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;

namespace MvcUtil
{
    public class XMLHelper
    {

        public static string getInnerText(XmlElement pvXmlEle_Parent)
        {
            return "";
        }

        public static string getInnerText(XmlElement pvXmlEle_Parent, string pvNodeName)
        {
            return (pvXmlEle_Parent.GetElementsByTagName(pvNodeName).Count > 0)
                       ? pvXmlEle_Parent.GetElementsByTagName(pvNodeName)[0].InnerText
                       : "";
        }

        public static XmlElement getElement(XmlElement pvXmlEle_Parent, string pvNodeName)
        {
            return (pvXmlEle_Parent.GetElementsByTagName(pvNodeName).Count > 0)
                       ? (XmlElement)pvXmlEle_Parent.GetElementsByTagName(pvNodeName)[0]
                       : null;
        }

        public static XmlElement newElement(XmlDocument pvXmlDocContext, string pvNodeName, string pvInnerText)
        {
            XmlElement _xmlEle = pvXmlDocContext.CreateElement(pvNodeName);
            _xmlEle.InnerText = pvInnerText;
            return _xmlEle;
        }

        public static XmlElement newElement(XmlDocument pvXmlDocContext, string pvNodeName)
        {
            return pvXmlDocContext.CreateElement(pvNodeName);
        }

        public static XmlAttribute newAttribute(XmlDocument pvXmlDoc, string pvKey, string pvValue)
        {
            XmlAttribute _xmlAttr = pvXmlDoc.CreateAttribute(pvKey);
            _xmlAttr.Value = pvValue;
            return _xmlAttr;
        }

        public static XmlElement GetElement(string xml)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            return doc.DocumentElement;
        }
    }

    public class Tools
    {
        static string pathLog = @"C:\Log\Pincards\";
        static object syncObj = new object();
        public static void Logger(String mess, String name, String functionName)
        {
            lock (syncObj)
            {
                try
                {
                    Directory.CreateDirectory(pathLog + DateTime.Now.ToString("yyyy-MM-dd"));
                    StreamWriter sw = new StreamWriter(pathLog + DateTime.Now.ToString("yyyy-MM-dd") + "\\" + DateTime.Now.ToString("MM-dd-HH") + name + ".txt", true);

                    sw.WriteLine(DateTime.Now.ToString("hh-mm-ss") + " : [" + functionName + "] :: " + mess);

                    sw.Flush();
                    sw.Close();
                }
                catch
                { }
            }
        }
    }
}
